﻿using UnityEditor;
using UnityEditor.Graphs;
using UnityEngine;

public static class SM_DrawBGGUIUtils
{
    public static Rect bgRect= new Rect(-SM_EditorConstValue.Bg_GUI_ExtraLenth / 2, -SM_EditorConstValue.Bg_GUI_ExtraLenth / 2,600,600);
   
    public static Rect controlWindowRange;
    public static void BeginGUI(Rect controlRange, Rect nodeMaxRange)
    {
        ResizeBGGUI();

        controlWindowRange = controlRange;
        ComputeBGRect(nodeMaxRange);
        GUILayout.BeginArea(controlRange, Styles.graphBackground);
        GUILayout.BeginArea(bgRect, Styles.graphBackground);
        DrawBackGroundGrid();
        
    }
    public static void EndGUI()
    {
        
        GUILayout.EndArea();
        GUILayout.EndArea();
    }

    private static Rect ComputeBGRect(Rect nodeMaxRange)
    {
       float extraLenth= SM_EditorConstValue.Bg_GUI_ExtraLenth;
        float tempxMin = -extraLenth / 2;
        bgRect. xMin = Mathf.Min(bgRect.xMin, Mathf.Min(tempxMin, nodeMaxRange.xMin - extraLenth));
        float tempxMax = bgRect. xMin + controlWindowRange.width + extraLenth; ;
        bgRect.xMax = Mathf.Max(bgRect.xMax, Mathf.Max(tempxMax, nodeMaxRange.xMax + extraLenth));
        float tempyMin = -extraLenth / 2;
        bgRect.yMin = Mathf.Min(bgRect.yMin, Mathf.Min(tempyMin, nodeMaxRange.yMin - extraLenth));
        float tempyMax = bgRect.yMin + controlWindowRange.height + extraLenth; ;
        bgRect.yMax = Mathf.Max(bgRect.yMax, Mathf.Max(tempyMax, nodeMaxRange.yMax + extraLenth));

        Vector2 v2 = bgRect.position + Control();
        float xC = (bgRect.width - controlWindowRange.width) / 2;
        float yC = (bgRect.height - controlWindowRange.height) / 2;
        v2.x = Mathf.Clamp(v2.x, -xC, 0);
        v2.y = Mathf.Clamp(v2.y, -yC, 0);
        bgRect.position = v2;

        return bgRect;
    }
    private static Vector2 Control()
    {
        Vector2 v2 = Vector2.zero;
        Event e = Event.current;
        if (e.isMouse)
        {
            if (e.button == 2 && e.type == EventType.MouseDrag && controlWindowRange.Contains(e.mousePosition))
            {
                v2 = e.delta;
                e.Use();
            }
        }
        return v2;
    }
    private static void ResizeBGGUI()
    {
        Event e = Event.current;
        if (controlWindowRange.Contains(e.mousePosition) && e.type== EventType.ScrollWheel)
        {
            SM_EditorConstValue.RatioGUI += e.delta.y > 0 ? -0.05f : 0.05f;
            e.Use();
            //Debug.Log(e.delta);
        }
    }
    public static void DrawBackGroundGrid()
    {
        DrawGridLines(bgRect, SM_EditorConstValue.BG_Line_Size0, new Color(0.4f, 0.4f, 0.4f, 1));
        DrawGridLines(bgRect, SM_EditorConstValue.BG_Line_Size1, Color.black);

        StateMachineUtils.HandleColorChangeEnd();
    }

    public static void DrawGridLines(Rect range, float gridSize, Color gridColor)
    {
        Handles.color = gridColor;
        float xMin = range.x;
        float xMax = range.x + range.width;
        float yMin = range.y;
        float yMax = range.y + range.height;
        int wNum = (int)(range.width / gridSize);
        int hNum = (int)(range.height / gridSize);
        for (int i = 0; i < wNum; i++)
        {
            Handles.DrawLine(new Vector2(i * gridSize, yMin), new Vector2(i * gridSize, yMax));
        }
        for (int i = 0; i < hNum; i++)
        {
            Handles.DrawLine(new Vector2(xMin, i * gridSize), new Vector2(xMax, i * gridSize));
        }

    }
}